﻿using System;
using System.Threading.Tasks;
using ConsoleApp.Job.TaskFramework;
using ConsoleAppFramework.Util;

namespace ConsoleAppFramework.Job.TaskJob
{
    public class BatchJob : TaskBase
    {
        public async override void Execute()
        {
            var batcher = new Batcher<string>(processor: Process,batchSize: 1000, interval: TimeSpan.FromSeconds(2));
            var random = new Random();
            while (true)
            {
                var count = random.Next(1, 4);
                for (var i = 0; i < count; i++)
                {
                    batcher.Add(Guid.NewGuid().ToString());
                }
                await Task.Delay(1000);
            }

            static void Process(Batch<string> batch)
            {
                using (batch)
                {
                    Console.WriteLine(
                       $"[{DateTimeOffset.Now}]{batch.Count} items are delivered.");
                }
            }
        }
    }
}
